/**
 *
 * @author 
 *
 */
class InputPMView extends eui.Component{
    
    private im_close:eui.Image;
    private btn_close:eui.Rect;
    private input_ph:eui.EditableText;
    private btn_tijiao:eui.Image;
    public static audioSoundChannel: egret.SoundChannel;
    private static audio: egret.Sound = new egret.Sound();
    
	public constructor() {
    	    super();
          this.skinName = "resource/skins/InputPMSkin.exml";
	}
	
    public createChildren() {
        super.createChildren();
        this.btn_close.addEventListener(egret.TouchEvent.TOUCH_TAP,this.sendPM,this);
        this.btn_tijiao.addEventListener(egret.TouchEvent.TOUCH_TAP,this.sendPM,this);
        this.init();
    }
    
    private init() {
        egret.Tween.get(this,{ loop: true }).to({ t: Math.PI * 2 },800);
    }
    private _t: number = 0;
    get t(): number { return this._t; }
    set t(value: number) {
        this._t = value;
        this.im_close.scaleX = Math.sin(value) * 0.05 + 1;
        this.im_close.scaleY = Math.sin(value - Math.PI / 2) * 0.05 + 1;
        this.btn_tijiao.scaleX = Math.sin(value) * 0.05 + 1;
        this.btn_tijiao.scaleY = Math.sin(value - Math.PI / 2) * 0.05 + 1;
    }
    
    private sendPM(ev:egret.TouchEvent){
        switch(ev.target){
            case this.btn_close:
                Main.getInstance().removeChild(this);
                GameOverView.getInstance().lb_results1.text = "您在规定的时间内找到了"+Data.grade.toString()+"部手机";
                if(this.input_ph.text =="请输入手机号"){
                    this.sendForPM("");
                }else{
                    if(this.input_ph.text.length!=11){
                        this.sendForPM("");
                    }else{
                        this.sendForPM(this.input_ph.text);
                    }
                }
                
            break;
            case this.btn_tijiao:
                if(this.input_ph.text.length<11){
                    alert("请输入正确的手机号码");
                }else{
                    Main.getInstance().removeChild(this);
                    this.sendForPM(this.input_ph.text);
                }
            break;
        }
    }
    
    private sendForPM(phoneNum:string){
        //给后端发请求
        var nickName = egret.localStorage.getItem("nickname");
//        alert(nickName);
        var openid = egret.localStorage.getItem("openid");
        var request = new egret.HttpRequest();
        request.responseType = egret.HttpResponseType.TEXT;
        request.open("http://ztetj7.weixinzjit.com/ZTE/index.php/User/add?" + "openid=" + openid+"&score="+Data.grade+"&nickname="+nickName+"&phone="+phoneNum,egret.HttpMethod.GET);
        request.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
        request.send();
        request.addEventListener(egret.Event.COMPLETE,this.onGetComplete1,this);
        request.addEventListener(egret.IOErrorEvent.IO_ERROR,this.onGetIOError1,this);
        request.addEventListener(egret.ProgressEvent.PROGRESS,this.onGetProgress1,this);
    }
    
    private onGetComplete1(event: egret.Event): void {
        var request = <egret.HttpRequest>event.currentTarget;
        var response = request.response;
        var ob = JSON.parse(response);
        GameOverView.getInstance().lb_results2.text = "排名第" + ob.rank + "位，击败了" + ob.percent+"的人。";
        Data.grade = 0;
    }
    private onGetIOError1(event: egret.IOErrorEvent): void {
        //        alert("get error : " + event);
    }
    private onGetProgress1(event: egret.ProgressEvent): void {

    }
    
}
